<?php
/**
 *
 * @param type $params
 * @return type 
 */
function create_pdf_file_dom($params = array()) {

    $CI = & get_instance();

    //Load the library
    $CI->load->library('html2pdf');
    
    //Set folder to save PDF to
    if (!file_exists('./files/' . $params['folder']))
        mkdir('./files/' . $params['folder']);
    
    $CI->html2pdf->folder('./files/' . $params['folder'] . '/');
    //Set the filename to save/download as
    $CI->html2pdf->filename($params['filename'] . '.pdf');
    //Set the paper defaults
    $CI->html2pdf->paper('a4', 'portrait');
    //Load html view
    $CI->html2pdf->html($params['html']); //$this->html2pdf->html($this->load->view('pdf', $data, true));

    if ($CI->html2pdf->create('save')) {
        //PDF was successfully saved or downloaded
        return $params['folder'] . '/' . $params['filename'] . '.pdf';
        // base_url() . '/files/' . $params['folder'] . '/' . $params['filename'] . '.pdf'; 
//        echo 'PDF saved';
    } else {
        return FALSE;
    }
}

/*
  Dùng pdf dom của CI 1 số browser nó ko chạy, anh chỉ làm đơn giản như vầy thôi, gọi hàm này đến chỗ nào cần phát sinh ra file pdf
  và truyền đúng param như vầy là ok
  $pdf_data = array(
  'folder' => 'ticket',
  'filename' => $ticket[0]->ticket_name . $random,
  'html' => $this->load->view('pdf', $pdf_ticket_view, TRUE)
  );
 */

function create_pdf_file($params = array()) {
    //Copy file folder html2pdf em đã gởi cho anh ra ngoài thư mục gốc để xài
    require_once('./html2pdf/html2pdf.class.php');
    $html2pdf = new HTML2PDF('P', 'A4', 'fr');
    $file_name = './files/' . $params['folder'] . '/' . $params['filename'] . '.pdf';
    if (file_exists($file_name))
        $file_name = './files/' . $params['folder'] . '/' . $params['filename'] . '_' . time() . '.pdf';
    $html2pdf->WriteHTML($params['html']);
    $html2pdf->Output($file_name, 'F');
    return $params['filename'] . '.pdf';
}